package com.stlm2.dems.service;

import com.stlm2.core.common.page.PageView;
import com.stlm2.dems.dao.UserDao;
import com.stlm2.dems.entity.User;
import com.stlm2.util.Convert;
import com.stlm2.util.StringUtils;
import com.stlm2.util.codecontent.CodeContent;
import org.apache.commons.lang.math.NumberUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;

import com.stlm2.dems.entity.KeFu;
import com.stlm2.dems.dao.KeFuDao;
import com.stlm2.core.base.BaseDao;
import com.stlm2.core.base.impl.BaseServiceImpl;
import org.springframework.ui.ModelMap;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("keFuService")
@Transactional(rollbackForClassName={"java.lang.Exception"}, propagation = Propagation.REQUIRED)
public class KeFuService extends BaseServiceImpl<KeFu> {

	@Resource
	private KeFuDao keFuDao;

	@Resource
	private UserDao userDao;

	@Override
	protected BaseDao <KeFu> getDao() {
		return keFuDao;
	}



	/**
	 *  后台添加前台人工客服
	 * ffl
	 * 2017-12-14 10:43:36
	 * @param token
	 * @param weChatNumber
	 * @param weChatCode
	 * @return
	 */
	public String addKeFu(String token,String weChatNumber,String weChatCode){
		User user=userDao.getByToken(token);
		if (user==null){
			return CodeContent.getModelByCode(-1,"",null);
		}
		if(StringUtils.isNullOrEmpty(weChatNumber)){
			return CodeContent.getModelByCode(0,"微信号不能为空",null);
		}
		if(StringUtils.isNullOrEmpty(weChatCode)){
			return CodeContent.getModelByCode(0,"微信二维码名片不能为空",null);
		}
		ModelMap model=new ModelMap();
		List<KeFu> old=keFuDao.getByNumber(weChatNumber);
		if (old!=null&&old.size()>0){//该用户已添加过该商品
			return CodeContent.getModelByCode(1,"",model);
		}
		KeFu keFu=new KeFu();
		keFu.setWeChatNumber(weChatNumber);
		keFu.setWeChatCode(weChatCode);
		keFu.setState(true);
		keFu.setCreateTime(new Date());
		keFu.setCreateUserId(user.getId());
		keFuDao.add(keFu);
		return CodeContent.getModelByCode(1,"",model);
	}


	/**
	 *
	 *  后台编辑前台人工客服
	 * ffl
	 * 2017-12-14 10:59:32
	 * @param token
	 * @param id
	 * @param weChatNumber
	 * @param weChatCode
	 * @param state
	 * @return
	 */
	public String editKeFu(String token,String id,String weChatNumber,String weChatCode,Boolean state){
		User user=userDao.getByToken(token);
		if (user==null){
			return CodeContent.getModelByCode(-1,"",null);
		}
		if(!NumberUtils.isDigits(id)){
			return CodeContent.getModelByCode(0,"未传入合法的客服记录id",null);
		}
		if(StringUtils.isNullOrEmpty(weChatNumber)){
			return CodeContent.getModelByCode(0,"微信号不能为空",null);
		}
		if(StringUtils.isNullOrEmpty(weChatCode)){
			return CodeContent.getModelByCode(0,"微信二维码名片不能为空",null);
		}
		ModelMap model=new ModelMap();
		KeFu keFu=keFuDao.getById(NumberUtils.toInt(id));
		if (keFu==null){
			return CodeContent.getModelByCode(0,"未找到id为"+id+"该客服的记录",null);
		}

		keFu.setWeChatNumber(weChatNumber);
		keFu.setWeChatCode(weChatCode);
		if(state!=null) {
			keFu.setState(state);
		}
		keFuDao.modify(keFu);
		return CodeContent.getModelByCode(1,"",model);
	}


	/**
	 * 后台列表页
	 * ffl
	 * 2017-12-14 11:17:30
	 * @param model
	 * @param page
	 * @param token
	 * @param weChatNumber
	 * @param state
	 * @return
	 */
	public String getCrmList(ModelMap model, String page,String token,String weChatNumber,Boolean state){
		PageView pageview = new PageView();
		int pageNo = 1;
		int pageSize = 20;
		if (NumberUtils.isDigits(page) && !"0".equals(page.trim())) {
			pageNo = NumberUtils.toInt(page);
		}
		if (org.springframework.util.StringUtils.isEmpty(token)) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		User loginUser = userDao.getByToken(token);
		if (loginUser == null) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		KeFu keFu=new KeFu();
		if (!StringUtils.isNullOrEmpty(weChatNumber)) {
			keFu.setWeChatNumber(weChatNumber.trim());
		}

		keFu.setState(state);
		pageview=keFuDao.queryPageView(pageNo, pageSize,keFu);
		model.put("code",1);
		model.put("msg","success");
		if (pageview != null){
			model.put("datas",convertCrmList(pageview.getRecords()));
			model.put("recordsTotal", pageview.getRowCount());
			model.put("totalPage", pageview.getPageCount());
			model.put("pageNow", pageview.getPageNow());
		}
		return  new JSONObject(model).toString();
	}

	public List<ModelMap> convertCrmList(List<KeFu> keFuList) {
		List<ModelMap> list = new ArrayList<ModelMap>();
		if (keFuList != null &&keFuList.size() > 0) {
			for (KeFu keFu : keFuList) {
				ModelMap map = buildModelCrmList(keFu);
				list.add(map);
			}
		}
		return list;
	}
	public ModelMap buildModelCrmList(KeFu keFu) {
		ModelMap model = new ModelMap();
		model.put("id", keFu.getId());
		model.put("weChatNumber", keFu.getWeChatNumber());
		model.put("weChatCode", keFu.getWeChatCode());
		model.put("state",keFu.getState()?"有效":"无效");
		model.put("stateValue",keFu.getState()?1:0);
		model.put("createTime", keFu.getCreateTime() != null ? Convert.toFullYMDHMS(keFu.getCreateTime()) : "");
		User user=userDao.getById(keFu.getCreateUserId());
		String userName="";
		if(user!=null){
			userName=user.getRealName();
		}
		model.put("userName",userName);
		return  model;
	}


	/**
	 *  获取详情
	 * ffl
	 * 2017-12-14 12:05:34
	 * @param id
	 * @param token
	 * @return
	 */
	public String getCrmDetails(String id,String token){
		User user=userDao.getByToken(token);
		if (user==null){
			return CodeContent.getModelByCode(-1,"",null);
		}
		if (!NumberUtils.isDigits(id)){
			return CodeContent.getModelByCode(0,"未传入合法的客服记录id",null);
		}
		KeFu keFu=keFuDao.getById(NumberUtils.toInt(id));
		if (keFu==null){
			return CodeContent.getModelByCode(0,"未找到id为"+id+"该客服的记录",null);
		}
		return CodeContent.getModelByCode(1,"",buildModelCrmList(keFu));
	}


	/**
	 * 	展示一个人工客服的信息
	 *   ffl
	 *   2017-12-14 10:49:15
	 * @return
	 */
	public String getOne(){
		KeFu keFu=keFuDao.getOne();
		return CodeContent.getModelByCode(1,"",buildModel(keFu));
	}

	public ModelMap buildModel(KeFu keFu) {
		ModelMap model = new ModelMap();
		if(keFu!=null) {
			model.put("id", keFu.getId());
			model.put("weChatNumber", keFu.getWeChatNumber());
			model.put("weChatCode", keFu.getWeChatCode());
		}
		return  model;
	}


	/**
	 *  修改人工客服状态 是否有效
	 * ffl
	 * 2017-12-15 11:21:04
	 * @param token
	 * @param id
	 * @param state
	 * @return
	 */
	public String editKeFuState(String token,String id,Boolean state){
		User user=userDao.getByToken(token);
		if (user==null){
			return CodeContent.getModelByCode(-1,"",null);
		}
		if(!NumberUtils.isDigits(id)){
			return CodeContent.getModelByCode(0,"未传入合法的客服记录id",null);
		}
		if(state==null){
			return CodeContent.getModelByCode(0,"记录状态不能为空",null);
		}

		ModelMap model=new ModelMap();
		KeFu keFu=keFuDao.getById(NumberUtils.toInt(id));
		if (keFu==null){
			return CodeContent.getModelByCode(0,"未找到id为"+id+"该客服的记录",null);
		}



		keFu.setState(state);
		keFuDao.modify(keFu);
		return CodeContent.getModelByCode(1,"",model);
	}







}